import java.util.*;

class Node {
    int i;
    int j;
    public Node(int i, int j) {
        this.i = i;
        this.j = j;
    }
}

public class Test2 {
    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> grid = new ArrayList<>();

        ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(2);
        list1.add(1);
        list1.add(1);

        ArrayList<Integer> list2 = new ArrayList<>();
        list2.add(1);
        list2.add(0);
        list2.add(1);

        ArrayList<Integer> list3 = new ArrayList<>();
        list3.add(1);
        list3.add(1);
        list3.add(1);

        grid.add(list1);
        grid.add(list2);
        grid.add(list3);

        System.out.println(rotApple(grid));
    }
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param grid int整型ArrayList<ArrayList<>> 
     * @return int整型
     */
    public static int rotApple (ArrayList<ArrayList<Integer>> grid) {
        int m = grid.size();
        int n = grid.get(0).size();
        int count = 0;//记录好苹果的个数

        int time = 0;

        int[][] arr = new int[m + 2][n + 2];

        for (int i = 0; i < m + 2; i++) {//扩展数组
            for (int j = 0; j < n + 2; j++) {
                if ((i == 0 || i == m + 1) && (j == 0 || j == n + 1)) {
                    arr[i][j] = 0;
                }
                if ((i >= 1 && i <= m) && (j >= 1 && j <= n)) {
                    arr[i][j] = grid.get(i - 1).get(j - 1);
                    if (arr[i][j] == 1) {
                        count++;
                    }
                }
            }
        }
//        for (int i = 0; i < m + 2; i++) {
//            for (int j = 0; j < n + 2; j++) {
//                if (arr[i][j] == 1) {
//                    count++;
//                }
//            }
//        }
        System.out.println(count);

        Stack<Node> stack = new Stack<>();
        while (count > 0) {
            boolean flag = false;
            for (int i = 1; i < m + 1; i++) {//找到数组中的2并存到栈中
                for (int j = 1; j < n + 1; j++) {
                    Node node = new Node(i, j);
                    if (arr[i][j] == 2) {
                        stack.push(node);
                    }
                }
            }
//            for (Node node : stack) {
//                System.out.print(node.i + " " + node.j + ", ");
//            }
//            System.out.println();
            while (!stack.isEmpty()) {
                Node node = stack.pop();
                int k = node.i;
                int l = node.j;
                if (arr[k][l] == 2){
//                    flag = true;
                    if (arr[k - 1][l] == 1) {
                        arr[k - 1][l] = 2;
                        flag = true;
                        count--;
                    }
                    if (arr[k + 1][l] == 1) {
                        arr[k + 1][l] = 2;
                        flag = true;
                        count--;
                    }
                    if (arr[k][l - 1] == 1) {
                        arr[k][l - 1] = 2;
                        flag = true;
                        count--;
                    }
                    if (arr[k][l + 1] == 1) {
                        arr[k][l + 1] = 2;
                        flag = true;
                        count--;
                    }
                }
            }
            if (flag) {
                time++;
            } else {
                return -1;
            }
        }

        for (int i = 1; i < m + 1; i++) {
            for (int j = 1; j < n + 1; j++) {
                if (arr[i][j] == 1) {
                    return -1;
                }
            }
        }
        return time;
    }
}